// pages/orderList/orderList.js
const app = getApp()
var host = app.globalData.host;
var self = null;

Page({
  data: {
    themeColor: '',
    list: [],
    keyword: '',
    currentIndex: 0,
    listType: 0, // 0待付款 1已付款 2待收货 3团购 4卡券 5商品
    page: 1,
    size: 0,
    total: 0
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    self.setData({
      listType: Number(options.type)
    })
  },
  onShow: function () {
    self.init();
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  init() {
    var url = null;
    if (self.data.listType === 0) {
      url = host + '/yq/order/topay'
    } else if (self.data.listType === 1) {
      url = host + '/yq/order/paid'
    } else if (self.data.listType === 2) {
      url = host + '/yq/order/pending'
    } else if (self.data.listType === 3) {
      url = host + '/yq/order/groups'
    } else if (self.data.listType === 4) {
      url = host + '/yq/order/cards'
    } else if (self.data.listType === 5) {
      url = host + '/yq/order/products'
    }
    var params = {};
    var success = function (res) {
      console.log(res);
      self.setData({
        list: res.info,
        page: res.page,
        size: res.size,
        total: res.total
      })
    };
    var fail = function (res) {};
    var complete = function (res) {};
    app.request(url, params, success, fail, complete);
  },
  onKeywordChange(e) {
    self.setData({
      keyword: e.detail.value
    })
  },
  search(e) {

  },
  expand(e) {
    var index = e.currentTarget.dataset.index;
    var itemId = e.currentTarget.dataset.itemid;
    var orderId = e.currentTarget.dataset.orderid;
    if (Number(index) == self.data.currentIndex) {
      // wx.navigateTo({
      //   // url: '/pages/productInfo/productInfo?itemId=' + itemId
      //   url: '/pages/orderInfo/orderInfo?orderId=' + orderId
      // })
      return;
    }
    self.setData({
      currentIndex: Number(index)
    })
  },
  invite(e) {
    var productId = e.currentTarget.dataset.productid;
    wx.navigateTo({
      url: '/pages/pintuan/pintuan?productId=' + productId
    })
  },
  close(e) {
    var orderId = e.currentTarget.dataset.orderid;
    var url = host + '/yq/order/close';
    var params = {
      orderId: orderId
    };
    var success = function (res) {
      wx.showToast({
        title: '成功关闭订单',
        icon: 'none'
      })
      self.init();
    };
    var fail = function (res) { };
    var complete = function (res) { };
    wx.showModal({
      title: '提示',
      content: '确认要取消订单吗？',
      success: res => {
        if (res.confirm) {
          app.request(url, params, success, fail, complete);
        }
      }
    })
  },
  onShareAppMessage(object) {
    console.log(object);
    if (object.from == "button") {
      var itemId = object.target.dataset.itemid;
      var productId = object.target.dataset.productid;
      var groupId = object.target.dataset.groupid;
      var shareTitle = object.target.dataset.sharetitle;
      var shareImage = object.target.dataset.shareimage;
      return {
        title: shareTitle,
        imageUrl: shareImage,
        path: "/pages/productInfo/productInfo?itemId=" + itemId + "&productId=" + productId + "&groupId=" + groupId
      }
    } else {
      return null;
    }
  },
  showQRCode(e) {
    var url = e.currentTarget.dataset.url;
    if (!url) {
      return;
    }
    wx.previewImage({
      urls: [url]
    })
  },
  none(e) {
    return;
  }
})
